package com.jinhua.medical.manager.service;

import com.jinhua.medical.manager.mybatis.dto.AreaInfo;
import com.jinhua.medical.manager.mybatis.dto.BaseArea;
import com.jinhua.medical.manager.mybatis.dto.BaseAreaExample;
import com.jinhua.medical.manager.mybatis.mapper.BaseAreaMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 地区业务对象
 * Created by 王兴光 on 2018/6/27.
 */
@Service
public class BaseAreaService {
    @Resource
    private BaseAreaMapper baseAreaMapper;

    public List<AreaInfo> findAreaList(){
        List<AreaInfo> areaList = new ArrayList<AreaInfo>();

        // 定义条件,获取一级分类列表
        BaseAreaExample example1 = new BaseAreaExample();
        example1.getOredCriteria().add(example1.createCriteria().andParentidEqualTo(0));
        // 迭代一级分类列表
        for(BaseArea baseArea : baseAreaMapper.selectByExample(example1)){
            // 定义条件,根据一级分类编号获取二级分类列表
            BaseAreaExample example2 = new BaseAreaExample();
            example2.getOredCriteria().add(example2.createCriteria().andParentidEqualTo(baseArea.getCodeid()).andParentidBetween(1,100));
            List<AreaInfo> products = new ArrayList<AreaInfo>();
            for(BaseArea city : baseAreaMapper.selectByExample(example2)){
                products.add(new AreaInfo(city.getCodeid(),city.getParentid(),city.getCityname()));
            }
            areaList.add(new AreaInfo(baseArea.getCodeid(),baseArea.getParentid(),baseArea.getCityname(),products));
        }

        return areaList;
    }
}
